package OldFilesHemanth;

import java.io.*;
import java.net.*;
import java.util.*;



class Car_Riders extends Thread
 
{

 public int iden;
 public Ride d;
 public Random gen = new Random();

 public Car_Riders(String ThreadName, Ride d1, int id)
    {

      iden = id;
      d = d1;
      setName(ThreadName); 
      start();
     }

public void run()
    {
      if (getName().equals("rider")) 
             rider(); 
      if (getName().equals("car")) 
             car(); 
    } 

public void rider()

{
 int i, temp;

 for (i = 1; i <= 15; i++)
   {

    // d.cars_queue[d.c_q_rear]=iden;
     //d.c_q_rear++;

     d.request_to_ride(iden);
     /* this will wait until the ride is over  */

     //sleep;

     try{sleep(500);
          } catch(Exception e){ System.out.println(e);}
     
     }
 System.out.println("                        RIDER"+iden+" HAS FINISHED ROUNDS.");
  d.nOfRidersFinished++;
  if(d.nOfRidersFinished==d.Num1-1)
  {
        System.out.println("ONLY ONE RIDER IS LEFT...OTHER RIDERS HAVE FINISHED THEIR ROUNDS...SO THE RIDER HAS TO WAIT CONTINUOUSLY IF HE IS NOT IN THE PARK ALREADY");
        System.exit(0);
  }
}

public void car()
{
    int i;

 for (i = 1; i <= 5; i++)
   {
       d.request_car_boarding(iden); /*car must wait until riders have boarded */
       
       d.request_car_leaving(iden);    /*car must wait until less than two cars are in the part */

      // sleep  /*to simulate duration of a ride */
       
       try{sleep(gen.nextInt(1000));
          } catch(Exception e){ System.out.println(e);}

       d.request_car_exit(iden);  /* car must wait until all riders exit */

       //sleep /* to simulate time to refresh and come back again */

         try{sleep(500);
          } catch(Exception e){ System.out.println(e);}
   }

     System.out.println("                     CAR"+iden+" HAS FINISHED ROUNDS");
    d.nOfCarsFinished++;
  if(d.nOfCarsFinished==d.Num2)
  {
        System.out.println("ALL CARS HAVE FINISHED THEIR ROUNDS..SO REMAINING RIDERS HAVE TO WAIT CONTINUOUSLY IF THEY ARE NOT IN THE PARK ALREADY.");
        System.exit(0);
  }

}
}



